﻿using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.DevicePlatform;
using HDSmartCommunity.Models;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace HDSmartCommunity.HDPlatform
{
    public class HDZHProcessData
    {
        public Socket server;
        public Thread thread;
        public int count = 1;//流水号
        public string devid;//主设备ID
        public string subdevid;//子设备ID
        public Dictionary<int, List<byte>> dic_data = new Dictionary<int, List<byte>>();
        public static MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions() { });
        public DevInfo devinfo;
        public static ParameterRead Parameter = new ParameterRead();
        public HDZHDeviceSerialData serial;
        public static object sendlocker = new object();
        public static object carstatuslocker = new object();
        public int heartcount = 0;
        public static CancellationTokenSource tokenSource = new CancellationTokenSource();
        public CancellationToken token = tokenSource.Token;
        public ManualResetEvent resetEvent = new ManualResetEvent(true);
        public static int hdstatus = 0;//恒大平台连接状态，1：注册中，2：心跳发送中
        System.Timers.Timer t;
        private static bool isReg = false;
        private static bool firstReg = true;
        public LogPrint logPrint;
        public int reporttime = 0;
        System.Timers.Timer reporttimer = null;
        public bool isreport = false;
        public static ElevatorStatusInfo ElevatorStatusInfo = new ElevatorStatusInfo();
        public string ip;
        public int interval;
        public static CancellationTokenSource report_tokenSource = new CancellationTokenSource();
        public CancellationToken report_token = report_tokenSource.Token;
        public ManualResetEvent report_resetEvent = new ManualResetEvent(true);

        public string StringToHex(string strEncode)
        {
            string strReturn = "";
            foreach (short shortx in strEncode.ToCharArray())
            {
                strReturn += shortx.ToString("X2") + " ";
            }
            strReturn = strReturn.TrimEnd(' ').ToUpper();
            return strReturn;
        }

        public string IntToHex(int num)
        {
            string result = "";
            result = Convert.ToString(num, 16).ToUpper();
            return result;
        }

        #region 连接恒大服务器

        public bool ConnectServer(string serverIP, int port)
        {
            try
            {
                server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Connect(serverIP, port);
                //server.ReceiveTimeout = 10000;
                thread = new Thread(StartReceive);
                thread.Start(server);
                return true;
            }
            catch (Exception e)
            {
                // logPrint.Log("连接恒大异常");
                return false;
            }
        }

        #endregion

        #region 接收恒大平台消息
        public void StartReceive(object obj)
        {
            byte[] buffer = new byte[2048];
            int length = 0;
            while (true)
            {
                try
                {
                    length = server.Receive(buffer);
                    if (length == 0)
                    {
                        break;
                    }
                    else
                    {
                        if (buffer[0] == 0x48 && buffer[1] == 0x44 && buffer[2] == 0x58 && buffer[3] == 0x4D)
                        {
                            List<byte> data = new List<byte>();

                            string strvalue = buffer[49].ToString("X").PadLeft(2, '0') + buffer[50].ToString("X").PadLeft(2, '0') + buffer[51].ToString("X").PadLeft(2, '0') + buffer[52].ToString("X").PadLeft(2, '0');
                            int value = int.Parse(strvalue, System.Globalization.NumberStyles.HexNumber) + 57;
                            string pack_hex = buffer[45].ToString("X2").PadLeft(2, '0') + buffer[46].ToString("X2").PadLeft(2, '0') + buffer[47].ToString("X2").PadLeft(2, '0') + buffer[48].ToString("X2").PadLeft(2, '0');
                            int packnum = Convert.ToInt32(pack_hex, 16);
                            for (int j = 0; j < length; j++)
                            {
                                if (j < value)
                                {
                                    data.Add(buffer[j]);
                                }
                            }
                            try
                            {
                                ReceiveData message = AnalysisType(data);
                                if (message != null)
                                {
                                    //下发凭证
                                    if (message.Command.ToUpper() == "COM_LOAD_CERTIFICATE")
                                    {
                                        CardInfo card = GetVoucherInfo(message.Data[0].ToString());
                                        if (card != null)
                                        {
                                            bool result = false;
                                            VoucherInfo voucher = QueryVoucher(card);
                                            if (voucher == null)
                                            {
                                                result = InsertVoucher(card);
                                            }
                                            else
                                            {
                                                result = UpdateVoucher(card);
                                            }

                                            Message replay = new Message();
                                            replay.Command = "COM_LOAD_CERTIFICATE";
                                            if (result)
                                            {
                                                replay.Result = 0;
                                            }
                                            else
                                            {
                                                replay.Result = 1;
                                            }
                                            string str = JsonConvert.SerializeObject(replay);
                                            string datazs = string.Format("{0} {1} {2} {3}", data[45].ToString("X").PadLeft(2, '0'), data[46].ToString("X").PadLeft(2, '0'), data[47].ToString("X").PadLeft(2, '0'), data[48].ToString("X").PadLeft(2, '0'));
                                            string strCmd = StringToHex(str);
                                            int num = strCmd.Split(' ').Length;
                                            string datalength = IntToHex(num).PadLeft(8, '0').ToUpper();
                                            string length1 = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                                            string crc = CRCCalc(strCmd);
                                            strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length1 + " 00 00 " + crc + " " + strCmd;
                                            logPrint.Log(strCmd);
                                            SendMsg(strCmd, false, false);
                                        }
                                    }
                                    //删除凭证
                                    else if (message.Command.ToUpper() == "COM_DELETE_CERTIFICATE")
                                    {
                                        CardInfo card = GetVoucherInfo(message.Data[0].ToString());
                                        if (card != null)
                                        {
                                            bool result = DelVoucher(card);

                                            Message replay = new Message();
                                            replay.Command = "COM_DELETE_CERTIFICATE";
                                            if (result)
                                            {
                                                replay.Result = 0;
                                            }
                                            else
                                            {
                                                replay.Result = 1;
                                            }
                                            string str = JsonConvert.SerializeObject(replay);
                                            string datazs = string.Format("{0} {1} {2} {3}", data[45].ToString("X").PadLeft(2, '0'), data[46].ToString("X").PadLeft(2, '0'), data[47].ToString("X").PadLeft(2, '0'), data[48].ToString("X").PadLeft(2, '0'));
                                            string strCmd = StringToHex(str);
                                            int num = strCmd.Split(' ').Length;
                                            string datalength = IntToHex(num).PadLeft(8, '0');
                                            string length1 = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                                            string crc = CRCCalc(strCmd);
                                            strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length1 + " 00 00 " + crc + " " + strCmd;
                                            SendMsg(strCmd, false, false);

                                        }
                                    }
                                    //清除所有凭证
                                    else if (message.Command.ToUpper() == "COM_DELETE_CERTIFICATE_IN_BATCH")
                                    {
                                        CardInfo card = GetVoucherInfo(message.Data[0].ToString());
                                        if (card != null)
                                        {
                                            bool result = DelAllTypeVoucher(card.CredenceType);

                                            Message replay = new Message();
                                            replay.Command = "COM_DELETE_CERTIFICATE_IN_BATCH";
                                            if (result)
                                            {
                                                replay.Result = 0;
                                            }
                                            else
                                            {
                                                replay.Result = 1;
                                            }
                                            string str = JsonConvert.SerializeObject(replay);
                                            string datazs = string.Format("{0} {1} {2} {3}", data[45].ToString("X").PadLeft(2, '0'), data[46].ToString("X").PadLeft(2, '0'), data[47].ToString("X").PadLeft(2, '0'), data[48].ToString("X").PadLeft(2, '0'));
                                            string strCmd = StringToHex(str);
                                            int num = strCmd.Split(' ').Length;
                                            string datalength = IntToHex(num).PadLeft(8, '0');
                                            string length1 = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                                            string crc = CRCCalc(strCmd);
                                            strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length1 + " 00 00 " + crc + " " + strCmd;

                                            SendMsg(strCmd, false, false);
                                        }
                                    }
                                    //参数读取
                                    else if (message.Command.ToUpper() == "COM_READ_PARAMETER")
                                    {
                                        Parameter.deviceID = devid;
                                        Parameter.workMode = serial.GetMode().ToString();
                                        ParameterReadMessage replay = new ParameterReadMessage();
                                        replay.Command = "COM_READ_PARAMETER";
                                        replay.Data = Parameter;
                                        string str = JsonConvert.SerializeObject(replay);
                                        string datazs = string.Format("{0} {1} {2} {3}", data[45].ToString("X").PadLeft(2, '0'), data[46].ToString("X").PadLeft(2, '0'), data[47].ToString("X").PadLeft(2, '0'), data[48].ToString("X").PadLeft(2, '0'));
                                        string strCmd = StringToHex(str);
                                        int num = strCmd.Split(' ').Length;
                                        string datalength = IntToHex(num).PadLeft(8, '0');
                                        string length1 = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                                        string crc = CRCCalc(strCmd);
                                        strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length1 + " 00 00 " + crc + " " + strCmd;

                                        SendMsg(strCmd, false, false);
                                    }
                                    //参数设置
                                    else if (message.Command.ToUpper() == "COM_SETTING_PARAMETERS")
                                    {
                                        ParameterSet parameterSet = GetParameterSet(message.Data[0].ToString());
                                        if (parameterSet != null)
                                        {
                                            devinfo = GetDevInfo();
                                            Parameter.fileServerUrl = parameterSet.fileServerUrl;
                                            Parameter.ntpServer = parameterSet.ntpServer;
                                            Parameter.httpUserName = parameterSet.httpUserName;
                                            Parameter.httpPassword = parameterSet.httpPassword;
                                            Parameter.liftCarNumber = parameterSet.liftCarNumber;
                                            Parameter.workMode = parameterSet.workMode;
                                            Parameter.lightMode = parameterSet.lightMode;
                                            Parameter.intervalTime = parameterSet.intervalTime;
                                            reporttime = Convert.ToInt32(parameterSet.intervalTime);

                                            LogHelper.Log("恒大下发梯控模式"+parameterSet.workMode.ToString());
                                            int mode = 0;
                                            if (parameterSet.workMode == "0")
                                            {
                                                mode = 1;
                                            }
                                            serial.UpdateLightMode(Convert.ToInt32(parameterSet.lightMode));
                                            serial.UpdateHdStatus(mode);
                                            serial.HDTK(mode, "恒大参数设置");
                                            if (reporttime > 0)
                                            {
                                                if (reporttimer == null)
                                                {
                                                    CarStatusServer();
                                                }
                                                else
                                                {
                                                    reporttimer.Close();
                                                    CarStatusServer();
                                                }
                                            }
                                            else
                                            {
                                                if (reporttimer != null)
                                                {
                                                    reporttimer.Close();
                                                }
                                            }

                                            if (parameterSet.ntpServer != devinfo.ntpserver)
                                            {
                                                RaspberryPie pie = new RaspberryPie();
                                                //bool set_result = pie.SetNtp(parameterSet.ntpServer);
                                            }

                                            Message replay = new Message();
                                            replay.Command = "COM_SETTING_PARAMETERS";
                                            if (UpdateParameter(parameterSet))
                                            {
                                                replay.Result = 0;
                                            }
                                            else
                                            {
                                                replay.Result = 1;
                                            }
                                            string str = JsonConvert.SerializeObject(replay);
                                            string datazs = string.Format("{0} {1} {2} {3}", data[45].ToString("X").PadLeft(2, '0'), data[46].ToString("X").PadLeft(2, '0'), data[47].ToString("X").PadLeft(2, '0'), data[48].ToString("X").PadLeft(2, '0')); string strCmd = StringToHex(str);
                                            int num = strCmd.Split(' ').Length;
                                            string datalength = IntToHex(num).PadLeft(8, '0');
                                            string length1 = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                                            string crc = CRCCalc(strCmd);
                                            strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length1 + " 00 00 " + crc + " " + strCmd;

                                            SendMsg(strCmd, false, false);
                                        }
                                    }
                                    //批量查询轿厢状态
                                    else if (message.Command.ToUpper() == "FAC_LIFT_BA_STATUS")
                                    {

                                        StatusQuery statusQuery = GetCarStatusQuery(message.Data[0].ToString());
                                        if (statusQuery.carIDList.Contains(devinfo.liftid.ToString().PadLeft(2, '0')))
                                        {
                                            QueryAllCarStatus();
                                        }
                                    }
                                    //目录查询
                                    else if (message.Command.ToUpper() == "COM_QUERY_DIR")
                                    {
                                        CatalogueData msgdata = new CatalogueData();
                                        msgdata.Command = "COM_QUERY_DIR";
                                        CatalogueInfo catalogue = new CatalogueInfo();
                                        catalogue.deviceID = subdevid;
                                        catalogue.name = "OTIS" + devinfo.liftid.ToString();
                                        catalogue.subDeviceType = 3036;
                                        catalogue.version = serial.getversion();
                                        List<CatalogueInfo> list = new List<CatalogueInfo>();
                                        list.Add(catalogue);
                                        msgdata.Data = list;
                                        string datazs = string.Format("{0} {1} {2} {3}", data[45].ToString("X").PadLeft(2, '0'), data[46].ToString("X").PadLeft(2, '0'), data[47].ToString("X").PadLeft(2, '0'), data[48].ToString("X").PadLeft(2, '0'));
                                        string str = JsonConvert.SerializeObject(msgdata);
                                        string strCmd = StringToHex(str);
                                        int num = strCmd.Split(' ').Length;
                                        string datalength = IntToHex(num).PadLeft(8, '0');
                                        string length1 = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                                        string crc = CRCCalc(strCmd);
                                        strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length1 + " 00 00 " + crc + " " + strCmd;

                                        SendMsg(strCmd, false, false);
                                    }
                                    //人脸识别
                                    else if (message.Command.ToUpper() == "ISSUE_COMMAND")
                                    {
                                        CmdMessage cmd = GetCmdInfo(message.Data[0].ToString());
                                        if (cmd != null)
                                        {
                                            serial.SendCmdToTK(cmd.Cmd, cmd.DevNo, cmd.Address, cmd.DataLen);
                                        }
                                    }
                                    else
                                    {
                                        if (dic_data.ContainsKey(packnum))
                                        {
                                            dic_data.Remove(packnum);
                                        }
                                        dic_data.Add(packnum, data);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                continue;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
        }
        public string ToHexs(byte[] data)
        {
            if (data.Length <= 0) return "";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.Append(data[i].ToString("X2"));
                sb.Append(" ");
            }
            string str = sb.ToString();
            return str.Substring(0, str.Length - 1);
        }
        #endregion

        public DevInfo GetDevInfo()
        {
            string strSql = "SELECT * FROM devinfo";
            CommFN<DevInfo> fn = new CommFN<DevInfo>();
            return fn.GetData(strSql);
        }

        #region 数据解析  

        /// <summary>
        /// 解析类型
        /// </summary>
        public ReceiveData AnalysisType(List<byte> data1)
        {
            ReceiveData message;
            try
            {
                List<byte> data = data1.GetRange(57, data1.Count - 57);
                string result = string.Empty;
                result = Encoding.GetEncoding("utf-8").GetString(data.ToArray(), 0, data.Count);
                message = JsonConvert.DeserializeObject<ReceiveData>(result);
            }
            catch
            {
                return null;
            }
            return message;
        }

        /// <summary>
        /// 凭证解析
        /// </summary>
        public CardInfo GetVoucherInfo(string data)
        {
            CardInfo cardInfo;
            try
            {
                cardInfo = JsonConvert.DeserializeObject<CardInfo>(data);
            }
            catch
            {
                logPrint.Log("凭证为空");
                return null;
            }
            return cardInfo;
        }

        /// <summary>
        /// 参数设置解析
        /// </summary>
        public ParameterSet GetParameterSet(string data)
        {
            ParameterSet para;
            try
            {
                para = JsonConvert.DeserializeObject<ParameterSet>(data);
            }
            catch
            {
                return null;
            }
            return para;
        }

        /// <summary>
        /// 状态查询解析
        /// </summary>
        public StatusQuery GetCarStatusQuery(string data)
        {
            StatusQuery status;
            try
            {
                status = JsonConvert.DeserializeObject<StatusQuery>(data);
            }
            catch
            {
                return null;
            }
            return status;
        }

        /// <summary>
        /// 命令解析
        /// </summary>
        public CmdMessage GetCmdInfo(string data)
        {
            CmdMessage message;
            try
            {
                message = JsonConvert.DeserializeObject<CmdMessage>(data);
            }
            catch
            {
                return null;
            }
            return message;
        }

        /// <summary>
        /// 解析回复数据
        /// </summary>
        public InitReplayData AnalysisReplay(List<byte> data1)
        {
            InitReplayData message;
            try
            {
                List<byte> data = data1.GetRange(57, data1.Count - 57);
                string result = string.Empty;
                result = Encoding.GetEncoding("utf-8").GetString(data.ToArray(), 0, data.Count);
                message = JsonConvert.DeserializeObject<InitReplayData>(result);
            }
            catch
            {
                return null;
            }
            return message;
        }

        #endregion

        #region 发送消息、发送后回复消息解析
        public bool SendMsg(string str, bool isinitiative, bool isRegCmd)
        {
            lock (sendlocker)
            {
                try
                {
                    string senddata = string.Format(@"{0}", str);
                    bool result = false;
                    while (!result)
                    {
                        if (server.Connected == true)
                        {
                            if (isReg)
                            {
                                try
                                {
                                    server.Send(HexStringToByteArray(senddata));
                                }
                                catch
                                {
                                    isReg = false;
                                    return false;
                                }
                                if (isinitiative)
                                {
                                    if (count < 2147483647)
                                    {
                                        count++;
                                    }
                                    else
                                    {
                                        count = 1;
                                    }
                                }
                                return true;
                            }
                            else
                            {
                                if (isRegCmd)
                                {
                                    try
                                    {
                                        server.Send(HexStringToByteArray(senddata));
                                    }
                                    catch
                                    {
                                        isReg = false;
                                        return false;
                                    }
                                    if (isinitiative)
                                    {
                                        if (count < 2147483647)
                                        {
                                            count++;
                                        }
                                        else
                                        {
                                            count = 1;
                                        }
                                    }
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                        }
                        else
                        {
                            if (!firstReg)
                            {
                                t.Close();
                                if (isreport)
                                {
                                    reporttimer.Close();
                                }
                            }
                            isReg = false;
                            if (ConnectToHD(devinfo.serverIP, devinfo.serverport))
                            {
                                if (Init())
                                {
                                    isReg = true;
                                }
                            }
                        }
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public static byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            return buffer;
        }

        #endregion

        public string GetMACIp()
        {
            //本地计算机网络连接信息
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            //获取本机所有网络连接
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

            if (nics == null || nics.Length < 1)
            {
                return "";
            }

            var MACIp = "";
            foreach (NetworkInterface adapter in nics)
            {
                var adapterName = adapter.Name;

                var adapterDescription = adapter.Description;
                var NetworkInterfaceType = adapter.NetworkInterfaceType;
                if (adapterName == "eth0")
                {
                    PhysicalAddress address = adapter.GetPhysicalAddress();
                    byte[] bytes = address.GetAddressBytes();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        MACIp += bytes[i].ToString("X2");

                        if (i != bytes.Length - 1)
                        {
                            MACIp += "-";
                        }
                    }
                }
            }

            return MACIp;
        }

        public string CRCCalc(string data)
        {
            string[] datas = data.Split(' ');
            List<byte> bytedata = new List<byte>();

            foreach (string str in datas)
            {
                bytedata.Add(byte.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier));
            }
            byte[] crcbuf = bytedata.ToArray();
            //计算并填写CRC校验码
            int crc = 0xffff;
            int len = crcbuf.Length;

            for (int j = 0; j < len; j++)
            {
                crc = ((crc >> 8) | (crc << 8)) & 0xffff;
                crc ^= (crcbuf[j] & 0xff);
                crc ^= ((crc & 0xff) >> 4);
                crc ^= (crc << 12) & 0xffff;
                crc ^= ((crc & 0xff) << 5) & 0xffff;
            }
            crc = crc & 0xffff;

            string[] redata = new string[2];
            redata[1] = Convert.ToString((byte)((crc >> 8) & 0xff), 16);
            redata[0] = Convert.ToString((byte)((crc & 0xff)), 16);
            string a = redata[1].ToString().PadLeft(2, '0') + " " + redata[0].ToString().PadLeft(2, '0');
            return a.ToUpper();
        }

        #region 设备注册、目录查询、子设备上报、心跳上报

        public bool DevReister(DevInfo devinfo)
        {
            try
            {
                int packnum = count;
                if (!string.IsNullOrEmpty(devinfo.serverIP) && devinfo.serverport > 0)
                {
                    HDRegInfo data1 = new HDRegInfo();
                    data1.mac = devinfo.mac;
                    data1.manufacturer = "1011";
                    data1.ip = devinfo.localIP;
                    data1.mask = devinfo.mask;
                    data1.deviceID = "1011" + "2020" + data1.mac.Replace("-", "");
                    //logPrint.Log(data1.deviceID);
                    devid = data1.deviceID;
                    HDMessageData msgdata = new HDMessageData();
                    msgdata.Command = "COM_DEV_REGISTER";
                    List<HDRegInfo> list = new List<HDRegInfo>();
                    list.Add(data1);
                    msgdata.Data = list;
                    string str = JsonConvert.SerializeObject(msgdata);
                    string zs = IntToHex(packnum).PadLeft(8, '0');
                    string datazs = zs.Substring(0, 2) + " " + zs.Substring(2, 2) + " " + zs.Substring(4, 2) + " " + zs.Substring(6, 2);
                    string strCmd = StringToHex(str);
                    int num = strCmd.Split(' ').Length;
                    string datalength = IntToHex(num).PadLeft(8, '0');
                    string length = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                    string crc = CRCCalc(strCmd);
                    strCmd = "48 44 58 4D " + StringToHex(data1.deviceID) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length + " 00 00 " + crc + " " + strCmd;
                    return SuccessDev(strCmd, "COM_DEV_REGISTER");
                }
                return false;
            }
            catch (Exception e)
            {
                logPrint.Log(e.ToString());
                return false;
            }
        }

        public bool SubDevReport(DevInfo devinfo)
        {
            try
            {
                int packnum = count;
                if (!string.IsNullOrEmpty(devinfo.serverIP) && devinfo.serverport > 0)
                {
                    HDRegInfo data1 = new HDRegInfo();
                    string liftid = string.Empty;

                    data1.mac = devinfo.mac;
                    data1.ip = devinfo.localIP;
                    data1.mask = devinfo.mask;

                    liftid = devinfo.liftid.ToString("X").PadLeft(4, '0');
                    data1.manufacturer = "3063";

                    //data1.version = serial.getversion();
                    data1.deviceID = "3063" + data1.mac.Replace("-", "") + liftid;
                    subdevid = data1.deviceID;
                    LogHelper.Log("子设备id:" + subdevid);
                    HDMessageData msgdata = new HDMessageData();
                    msgdata.Command = "COM_UPLOAD_SUB_DEV";
                    List<HDRegInfo> list = new List<HDRegInfo>();
                    list.Add(data1);
                    msgdata.Data = list;
                    string str = JsonConvert.SerializeObject(msgdata);
                    string zs = IntToHex(packnum).PadLeft(8, '0'); ;
                    string datazs = zs.Substring(0, 2) + " " + zs.Substring(2, 2) + " " + zs.Substring(4, 2) + " " + zs.Substring(6, 2);
                    string strCmd = StringToHex(str);
                    int num = strCmd.Split(' ').Length;
                    string datalength = IntToHex(num).PadLeft(8, '0');
                    string length = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                    string crc = CRCCalc(strCmd);
                    strCmd = "48 44 58 4D " + StringToHex(data1.deviceID) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length + " 00 00 " + crc + " " + strCmd;

                    return SendMsg(strCmd, true, false);
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public bool SuccessDev(string strCmd, string type)
        {
            int sendcount = 0;
            if (server != null)
            {
                bool result = SendMsg(strCmd, true, true);

                if (result)
                {
                    //  logPrint.Log("注册发送成功");
                    while (true)
                    {
                        if (ReceiveData(type))
                        {
                            logPrint.Log("设备初始化成功");
                            return true;
                        }
                        sendcount++;
                        if (sendcount > 10)
                        {
                            return false;
                        }
                        Wait(1000);
                    }
                }
                else
                {
                    Wait(10000);
                }
            }
            return false;
        }

        public bool Wait(int second)
        {
            try
            {
                DateTime startTime = DateTime.Now;
                while (true)
                {
                    TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - startTime.Ticks);
                    if (secondSpan.TotalMilliseconds > second)
                    {
                        break;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool SuccessHeart(string strCmd, string type)
        {
            int count = 0;
            if (server != null)
            {
                bool result = SendMsg(strCmd, true, false);
                if (result)
                {
                    while (count < 3)
                    {
                        if (ReceiveData(type))
                        {
                            logPrint.Log("心跳接收成功");
                            return true;
                        }
                        Wait(100);
                        count++;
                    }
                    return false;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public bool CheckHeart()
        {
            try
            {
                SendData msgdata = new SendData();
                msgdata.Data = null;
                msgdata.Command = "COM_HEARTBEAT";
                int packnum = count;
                string zs = IntToHex(packnum).PadLeft(8, '0'); ;
                string datazs = zs.Substring(0, 2) + " " + zs.Substring(2, 2) + " " + zs.Substring(4, 2) + " " + zs.Substring(6, 2);
                string str = JsonConvert.SerializeObject(msgdata);
                string strCmd = StringToHex(str);
                int num = strCmd.Split(' ').Length;
                string datalength = IntToHex(num).PadLeft(8, '0');
                string length = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                string crc = CRCCalc(strCmd);
                strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length + " 00 00 " + crc + " " + strCmd;

                return SuccessHeart(strCmd, "COM_HEARTBEAT");
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool ReceiveData(string messagetype)
        {
            foreach (var item in dic_data)
            {
                try
                {
                    InitReplayData initReplay = new InitReplayData();
                    if (item.Value.Count != 0)
                    {
                        initReplay = AnalysisReplay(item.Value);
                        if (initReplay != null)
                        {
                            if (initReplay.Command == messagetype && initReplay.Result == 0)
                            {
                                dic_data.Remove(item.Key);
                                return true;
                            }
                        }
                    }
                }
                catch
                {
                    continue;
                }
            }

            return false;

        }
        #endregion

        #region 凭证操作

        public bool InsertVoucher(CardInfo cardInfo)
        {
            /*            if (cardInfo.UserType == 0)
                        {
                            cardInfo.UserType = 2;
                        }*/
            string strSql = string.Format(@"INSERT INTO cardinfo(cardID,StartTime,OverTime,subDeviceID,UserType,CredenceType,username,UUID,elevatorAuthFloor)VALUES('{0}','{1}','{2}','{3}',{4},{5},'{6}','{7}','{8}')", cardInfo.credenceNo, cardInfo.startTime, cardInfo.endTime, cardInfo.subDeviceID, 3, cardInfo.CredenceType, "", cardInfo.userID, string.Join(",", cardInfo.elevatorAuthFloor));
            CommFN<CardInfo> fn = new CommFN<CardInfo>();
            return fn.ExecFN(strSql);
        }

        public VoucherInfo QueryVoucher(CardInfo cardInfo)
        {
            string strSql = string.Format(@"SELECT * FROM cardinfo WHERE UUID='{0}'", cardInfo.userID);
            CommFN<VoucherInfo> fn = new CommFN<VoucherInfo>();
            return fn.GetData(strSql);
        }

        public bool UpdateVoucher(CardInfo cardInfo)
        {
            string strSql = string.Format(@"UPDATE cardinfo SET cardID='{0}',StartTime='{1}',OverTime='{2}',subDeviceID='{3}',UserType='{4}',CredenceType={5},username='{6}',elevatorAuthFloor='{7}' WHERE UUID='{8}'", cardInfo.credenceNo, cardInfo.startTime, cardInfo.endTime, cardInfo.subDeviceID, 3, cardInfo.CredenceType, "", string.Join(",", cardInfo.elevatorAuthFloor), cardInfo.userID);
            CommFN<VoucherInfo> fn = new CommFN<VoucherInfo>();
            return fn.ExecFN(strSql);
        }

        public bool DelVoucher(CardInfo cardInfo)
        {
            string strSql = string.Format(@"DELETE FROM cardinfo WHERE UUID='{0}'", cardInfo.userID);
            CommFN<VoucherInfo> fn = new CommFN<VoucherInfo>();
            return fn.ExecFN(strSql);
        }

        public bool DelAllTypeVoucher(int type)
        {
            string strSql = "DELETE FROM cardinfo";
            CommFN<VoucherInfo> fn = new CommFN<VoucherInfo>();
            return fn.ExecFN(strSql);
        }

        #endregion

        #region 参数操作
        public bool UpdateParameter(ParameterSet para)
        {
            string strSql = string.Format(@"UPDATE devinfo SET intervalTime={0},ntpserver='{1}'", Convert.ToInt32(para.intervalTime), para.ntpServer);
            CommFN<CardInfo> fn = new CommFN<CardInfo>();
            return fn.ExecFN(strSql);
        }
        #endregion

        /// <summary>
        /// 乘梯事件上报
        /// </summary>
        public void IncidentReporting(Incident incident)
        {
            try
            {
                incident.deviceID = subdevid;
                IncidentMessage replay = new IncidentMessage();
                List<Incident> list = new List<Incident>();
                list.Add(incident);
                replay.Command = "FAC_ELEVATOR_RECORD";
                replay.Data = list;
                string str = JsonConvert.SerializeObject(replay);
                string zs = IntToHex(count).PadLeft(8, '0'); ;
                string datazs = zs.Substring(0, 2) + " " + zs.Substring(2, 2) + " " + zs.Substring(4, 2) + " " + zs.Substring(6, 2);
                string strCmd = StringToHex(str);
                int num = strCmd.Split(' ').Length;
                string datalength = IntToHex(num).PadLeft(8, '0');
                string length = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                string crc = CRCCalc(strCmd);
                strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length + " 00 00 " + crc + " " + strCmd;

                SendMsg(strCmd, true, false);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 轿厢状态上报
        /// </summary>
        public void HDZHCarStatusReport()
        {
            lock (carstatuslocker)
            {
                ElevatorPoint point;
                serial.memoryCache.TryGetValue(devinfo.liftid, out point);
                CarStatus carStatus = new CarStatus();
                Data data = new Data();
                carStatus.Data = data;
                List<ElevatorStatus> list = new List<ElevatorStatus>();
                ElevatorStatus newstatus = new ElevatorStatus();
                newstatus.deviceID = subdevid;
                newstatus.carID = devinfo.liftid.ToString().PadLeft(2, '0');
                newstatus.physicalfloor = point.physicalfloor;
                newstatus.displayfloor = point.Showfloor;
                if (point.up == 1)
                {
                    newstatus.status = "01";
                }
                else if (point.down == 1)
                {
                    newstatus.status = "02";
                }
                else if (point.up == 0 && point.down == 0)
                {
                    newstatus.status = "00";
                }

                if (point.behinddooropening == 1)
                {
                    newstatus.doorStatus = "10";
                }
                else if (point.behinddooropened == 1)
                {
                    newstatus.doorStatus = "11";
                }
                else if (point.behinddoorcloseing == 1)
                {
                    newstatus.doorStatus = "12";
                }
                else if (point.behinddoorclosed == 1)
                {
                    newstatus.doorStatus = "13";
                }

                newstatus.errorStatus = point.falut;
                newstatus.errorMessage = "";
                newstatus.fireCtrlStatus = point.fire;

                list.Add(newstatus);

                carStatus.Command = "FAC_UPLOAD_BA_STATUS";
                carStatus.Data.statusList = list;
                carStatus.Data.timestamp = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                string str = JsonConvert.SerializeObject(carStatus);
                string zs = IntToHex(count).PadLeft(8, '0'); ;
                string datazs = zs.Substring(0, 2) + " " + zs.Substring(2, 2) + " " + zs.Substring(4, 2) + " " + zs.Substring(6, 2);
                string strCmd = StringToHex(str);
                int num = strCmd.Split(' ').Length;
                string datalength = IntToHex(num).PadLeft(8, '0');
                string length = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                string crc = CRCCalc(strCmd);
                strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length + " 00 00 " + crc + " " + strCmd;
                //SendMsg(strCmd, true, false);
            }
        }
        public void CarStatusReport(object source, ElapsedEventArgs e)
        {
            lock (carstatuslocker)
            {
                ElevatorPoint point;
                serial.memoryCache.TryGetValue(devinfo.liftid, out point);
                CarStatus carStatus = new CarStatus();
                Data data = new Data();
                carStatus.Data = data;
                List<ElevatorStatus> list = new List<ElevatorStatus>();
                ElevatorStatus newstatus = new ElevatorStatus();
                newstatus.deviceID = subdevid;
                newstatus.carID = devinfo.liftid.ToString().PadLeft(2, '0');
                newstatus.physicalfloor = point.physicalfloor;
                newstatus.displayfloor = point.Showfloor;
                if (point.up == 1)
                {
                    newstatus.status = "01";
                }
                else if (point.down == 1)
                {
                    newstatus.status = "02";
                }
                else if (point.up == 0 && point.down == 0)
                {
                    newstatus.status = "00";
                }

                if (point.behinddooropening == 1)
                {
                    newstatus.doorStatus = "10";
                }
                else if (point.behinddooropened == 1)
                {
                    newstatus.doorStatus = "11";
                }
                else if (point.behinddoorcloseing == 1)
                {
                    newstatus.doorStatus = "12";
                }
                else if (point.behinddoorclosed == 1)
                {
                    newstatus.doorStatus = "13";
                }

                newstatus.errorStatus = point.falut;
                newstatus.errorMessage = "";
                newstatus.fireCtrlStatus = point.fire;

                list.Add(newstatus);

                carStatus.Command = "FAC_UPLOAD_BA_STATUS";
                carStatus.Data.statusList = list;
                carStatus.Data.timestamp = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                string str = JsonConvert.SerializeObject(carStatus);
                string zs = IntToHex(count).PadLeft(8, '0'); ;
                string datazs = zs.Substring(0, 2) + " " + zs.Substring(2, 2) + " " + zs.Substring(4, 2) + " " + zs.Substring(6, 2);
                string strCmd = StringToHex(str);
                int num = strCmd.Split(' ').Length;
                string datalength = IntToHex(num).PadLeft(8, '0');
                string length = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                string crc = CRCCalc(strCmd);
                strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length + " 00 00 " + crc + " " + strCmd;
                //SendMsg(strCmd, true, false);
            }
        }

        /// <summary>
        /// 电梯设备状态上报
        /// </summary>
        public void ElevatorDevReport(Device dev)
        {
            if (!string.IsNullOrEmpty(subdevid))
            {
                dev.deviceID = subdevid;
                List<Device> list = new List<Device>();
                list.Add(dev);
                ElevatorEquipment elevator = new ElevatorEquipment();
                elevator.Command = "FAC_UPLOAD_DEV_STATUS";
                elevator.Data = list;
                string str = JsonConvert.SerializeObject(elevator);
                string zs = IntToHex(count).PadLeft(8, '0'); ;
                string datazs = zs.Substring(0, 2) + " " + zs.Substring(2, 2) + " " + zs.Substring(4, 2) + " " + zs.Substring(6, 2);
                string strCmd = StringToHex(str);
                int num = strCmd.Split(' ').Length;
                string datalength = IntToHex(num).PadLeft(8, '0');
                string length = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
                string crc = CRCCalc(strCmd);
                strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length + " 00 00 " + crc + " " + strCmd;
                SendMsg(strCmd, true, false);
            }


        }

        /// <summary>
        /// 批量查询
        /// </summary>
        public bool QueryAllCarStatus()
        {
            CarStatus carStatus = new CarStatus();
            ElevatorPoint point = new ElevatorPoint();
            memoryCache = serial.GetStatusMemory();
            memoryCache.TryGetValue(devinfo.liftid, out point);
            ElevatorStatus status = new ElevatorStatus();
            if (point != null)
            {
                status.deviceID = subdevid;
                status.carID = devinfo.liftid.ToString().PadLeft(2, '0');
                status.physicalfloor = point.physicalfloor;
                status.displayfloor = point.Showfloor;
                if (point.up == 1)
                {
                    status.status = "01";
                }
                else if (point.down == 1)
                {
                    status.status = "02";
                }
                else
                {
                    status.status = "00";
                }

                if (point.frontdooropening == 1)
                {
                    status.doorStatus = "10";
                }
                else if (point.frontdooropened == 1)
                {
                    status.doorStatus = " 11";
                }
                else if (point.frontdoorcloseing == 1)
                {
                    status.doorStatus = " 12";
                }
                else if (point.frontdoorclosed == 1)
                {
                    status.doorStatus = " 13";
                }

                if (point.falut == 1)
                {
                    status.errorStatus = 1;
                }
                else
                {
                    status.errorStatus = 0;
                }
            }

            carStatus.Command = "FAC_LIFT_BA_STATUS";
            carStatus.Data.statusList.Add(status);
            string str = JsonConvert.SerializeObject(carStatus);
            string zs = IntToHex(count).PadLeft(8, '0'); ;
            string datazs = zs.Substring(0, 2) + " " + zs.Substring(2, 2) + " " + zs.Substring(4, 2) + " " + zs.Substring(6, 2);
            string strCmd = StringToHex(str);
            int num = strCmd.Split(' ').Length;
            string datalength = IntToHex(num).PadLeft(8, '0');
            string length = datalength.Substring(0, 2) + " " + datalength.Substring(2, 2) + " " + datalength.Substring(4, 2) + " " + datalength.Substring(6, 2);
            string crc = CRCCalc(strCmd);
            strCmd = "48 44 58 4D " + StringToHex(devid) + " " + StringToHex("00000000000000000000") + " 00 " + datazs + " " + length + " 00 00 " + crc + " " + strCmd;
            if (SendMsg(strCmd, true, false))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #region 连接恒大平台、设备注册、子设备上报、目录查询、心跳上报

        public void HDConnect()
        {
            devinfo.mac = GetLocalMac();
            devinfo.localIP = GetLocalIP();
            subdevid = "3063" + devinfo.mac.Replace("-", "") + devinfo.liftid.ToString("X").PadLeft(4, '0');
            //devinfo.mask = GetLocalNetInfo().Mask;
            Task task = new Task(async () =>
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
                resetEvent.WaitOne();
                //do something
                PowerOnConnection();

            }, token);
            task.Start();
        }

        public string GetLocalIP()
        {
            string localIP = string.Empty;
            try
            {
                using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
                {
                    socket.Connect("8.8.8.8", 65530);
                    IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
                    localIP = endPoint.Address.ToString();
                }
                return localIP;
            }
            catch
            {
                return "";
            }
        }

        public string GetMIniLocalIP()
        {
            try
            {
                string localip = string.Empty;
                StreamReader reader = new StreamReader(@"/etc/netplan/01-network-manager-all.yaml", Encoding.Default);
                string theReply = reader.ReadToEnd();
                reader.Close();
                Regex strRegex = new Regex(@"(?<str>([^\w\s]+|\s*)enp1s0:\s*dhcp4: no([\w\W]*))");
                Regex addressRegex = new Regex(@"([^\w\s]+|\s*)addresses:\s*[^\w\s](?<address>\d+.\d+.\d+.\d+/\d+)]\s*");
                Regex ipRegex = new Regex(@"\s*(?<ip>\d+.\d+.\d+.\d+)\s*");

                MatchCollection strCollection = strRegex.Matches(theReply);

                if (strCollection.Count > 0)
                {
                    string str_value = strCollection[0].Groups["str"].Value;
                    MatchCollection addressCollection = addressRegex.Matches(str_value);
                    for (int i = 0; i < addressCollection.Count; i++)
                    {
                        string str = addressCollection[i].Groups["address"].Value.Trim();
                        string ip = ipRegex.Matches(str)[0].Groups["ip"].Value.Trim();
                        localip = ip;
                    }

                }

                return localip;
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.ToString());
                return "";
            }
        }

        public string GetLocalMac()
        {
            string macAddress = "";
            try
            {
                foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (nic.OperationalStatus == OperationalStatus.Up)
                    {
                        macAddress = nic.GetPhysicalAddress().ToString();
                        IPInterfaceProperties ipProperties = nic.GetIPProperties();
                        UnicastIPAddressInformation curIP = ipProperties.UnicastAddresses[0];
                        string ip = curIP.Address.ToString();
                        string mask = curIP.IPv4Mask.ToString();
                        string server = ipProperties.GatewayAddresses[0].Address.ToString();
                        break;
                    }
                }
                return macAddress;
            }
            catch
            {
                return "";
            }
        }

        public void PowerOnConnection()
        {
            try
            {
                if (ConnectToHD(devinfo.serverIP, devinfo.serverport))
                {
                    logPrint.Log("连接恒大成功");
                    Init();
                }
            }
            catch
            {

            }
        }

        public bool ConnectToHD(string serverip, int port)
        {
            bool result = false;
            while (!result)
            {
                result = ConnectServer(serverip, port);
            }

            return result;
        }

        public bool Register(DevInfo devInfo)
        {
            bool result = false;
            hdstatus = 1;
            while (!result)
            {
                result = DevReister(devInfo);
            }
            return result;
        }

        public bool SubReportt(DevInfo devInfo)
        {
            bool result = false;
            while (!result)
            {
                result = SubDevReport(devInfo);
            }
            return result;
        }

        public bool Init()
        {
            if (Register(devinfo))
            {
                Equipment equipment = new Equipment();
                equipment.devtype = "恒大平台";
                equipment.state = 0;
                equipment.reason = "注册成功";
                equipment.actiontime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                InsertEquipment(equipment);

                isReg = true;
                //开启心跳上报定时服务
                HeartReport();

                if (SubReportt(devinfo))
                {
                    //开启轿厢状态上报
                    if (reporttime > 0)
                    {
                        CarStatusServer();
                    }
                    return true;
                }
            }
            return false;
        }

        public void HeartReport()
        {
            try
            {
                heartcount = 0;
                heart();
                t = new System.Timers.Timer(60000);//实例化Timer类，设置间隔时间为200毫秒；

                t.Elapsed += new ElapsedEventHandler(Check);//到达时间的时候执行事件；

                t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；

                t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
            }
            catch (Exception ex)
            {
                logPrint.Log("报错信息：" + ex.ToString());
                //throw;
            }
        }

        public void Check(object source, ElapsedEventArgs e)
        {
            try
            {
                hdstatus = 2;
                logPrint.Log("心跳间隔一分钟发送");
                if (CheckHeart())
                {
                    heartcount = 0;
                }
                else
                {
                    if (heartcount >= 2)
                    {
                        Equipment equipment = new Equipment();
                        equipment.devtype = "恒大平台";
                        equipment.state = 1;
                        equipment.actiontime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        equipment.reason = "心跳接收三次失败";
                        InsertEquipment(equipment);
                        logPrint.Log("心跳接收三次失败，重新注册");
                        t.Close();
                        if (isreport)
                        {
                            reporttimer.Close();
                        }
                        Init();
                    }
                    else
                    {
                        heartcount++;
                    }
                }
            }
            catch
            {
                heartcount++;
            }
        }

        public void InsertEquipment(Equipment equipment)
        {
            DeviceDAL da = new DeviceDAL();
            da.InsertEquipment(equipment);
        }

        public void InsertModeRecord(ModeRecord modeRecord)
        {
            DeviceDAL da = new DeviceDAL();
            da.InsertModeRecord(modeRecord);
        }

        public void heart()
        {
            logPrint.Log("心跳第一次发送");
            try
            {
                hdstatus = 2;

                if (!CheckHeart())
                {
                    heartcount++;
                }
            }
            catch
            {
                heartcount++;
            }
        }

        #endregion

        public string GetLocalIp()
        {
            try
            {
                //本地计算机网络连接信息
                IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
                //获取本机所有网络连接
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

                if (nics == null || nics.Length < 1)
                {
                    return "";
                }
                foreach (NetworkInterface adapter in nics)
                {
                    var adapterName = adapter.Name;
                    //LogHelper.Log(adapterName);
                    if (adapterName == "eth0")
                    {
                        //LogHelper.Log(adapter.GetIPProperties().UnicastAddresses[0].Address.ToString());
                        return adapter.GetIPProperties().UnicastAddresses[0].Address.ToString();
                    }
                }
                return "";

            }
            catch (Exception e)
            {
                return "";
            }
        }

        public string GetLocalMask()
        {
            try
            {
                //本地计算机网络连接信息
                IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
                //获取本机所有网络连接
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

                if (nics == null || nics.Length < 1)
                {
                    return "";
                }
                foreach (NetworkInterface adapter in nics)
                {
                    var adapterName = adapter.Name;
                    if (adapterName == "eth0")
                    {
                        return adapter.GetIPProperties().UnicastAddresses[0].IPv4Mask.ToString();
                    }
                }
                return "";

            }
            catch (Exception e)
            {
                return "";
            }
        }

        public NetInfo GetLocalNetInfo()
        {
            NetInfo netInfo = new NetInfo();
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                StreamReader reader = new StreamReader(@"/etc/dhcpcd.conf", Encoding.Default);
                string theReply = reader.ReadToEnd();
                reader.Close();
                Regex addressRegex = new Regex(@"(?<ip>([^\w\s]+|\s*)static\s*ip_address=\d+.\d+.\d+.\d+/\d+)\s*");
                Regex ipRegex = new Regex(@"=\s*(?<ip>\d+.\d+.\d+.\d+)\s*");
                Regex maskReg = new Regex(@"=\s*\d+.\d+.\d+.\d+/(?<mask>\d+)\s*");
                MatchCollection addressCollection = addressRegex.Matches(theReply);
                for (int i = 0; i < addressCollection.Count; i++)
                {
                    string str = addressCollection[i].Groups["ip"].Value.Trim();
                    if (!str.Contains("#"))
                    {
                        string ip = ipRegex.Matches(str)[0].Groups["ip"].Value.Trim();
                        netInfo.IP = ip;
                        int mask = Convert.ToInt32(maskReg.Matches(str)[0].Groups["mask"].Value.Trim());
                        netInfo.Mask = GetMask(mask);
                    }
                }
            }
            else
            {
                try
                {
                    string name = Dns.GetHostName();
                    IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
                    foreach (IPAddress ipa in ipadrlist)
                    {
                        if (ipa.AddressFamily == AddressFamily.InterNetwork)
                        {
                            netInfo.IP = ipa.ToString();
                            netInfo.Mask = "255.255.255.0";
                        }
                    }
                    /*//本地计算机网络连接信息
                    IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
                    //获取本机所有网络连接
                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

                    if (nics == null || nics.Length < 1)
                    {
                        return netInfo;
                    }
                    foreach (NetworkInterface adapter in nics)
                    {
                        var adapterName = adapter.Name;
                        //LogHelper.Log(adapterName);
                        if (adapterName == "本地连接")
                        {
                            netInfo.IP = adapter.GetIPProperties().UnicastAddresses[0].Address.ToString();
                            netInfo.Mask = adapter.GetIPProperties().UnicastAddresses[0].IPv4Mask.ToString();
                        }
                    }*/

                }
                catch (Exception e)
                {
                    return netInfo;
                }
            }

            return netInfo;
        }

        public string GetMask(int value)
        {
            try
            {
                string str = string.Empty;
                for (int i = 0; i < value; i++)
                {
                    str += "1";
                }
                str = str.PadRight(32, '0');
                string a = Convert.ToInt32(str.Substring(0, 8), 2).ToString() + "." + Convert.ToInt32(str.Substring(8, 8), 2).ToString() + "." + Convert.ToInt32(str.Substring(16, 8), 2).ToString() + "." + Convert.ToInt32(str.Substring(24, 8), 2).ToString();
                return a;
            }
            catch
            {
                return "0.0.0.0";
            }
        }

        public int hd()
        {
            return hdstatus;
        }

        public void CarStatusServer()
        {
            try
            {
                Task task = new Task(async () =>
                {
                    if (report_token.IsCancellationRequested)
                    {
                        return;
                    }
                    report_resetEvent.WaitOne();
                    //do something

                }, report_token);
                task.Start();
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }
            /* try
             {
                 reporttimer = new System.Timers.Timer(Convert.ToDouble(reporttime));

                 reporttimer.Elapsed += new ElapsedEventHandler(CarStatusReport);//到达时间的时候执行事件；

                 reporttimer.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；

                 reporttimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                 isreport = true;
             }
             catch (Exception ex)
             {
                 logPrint.Log("报错信息：" + ex.ToString());
             }*/
        }

        public ElevatorStatusInfo GetElevatorStatusInfo()
        {
            return ElevatorStatusInfo;
        }
    }
}
